﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace LibFriesland_Campina_CRM
{
    class DalProduct
    {   
        //Lay thong tin product theo id
        public static BizProduct getProductbyID(string pProduct_charid)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var product = db.Product.Single(p => p.ProductCharID == pProduct_charid);
                return new BizProduct(product.ProductCharID, product.ShortName, product.ByProductCatalogue);
            }
            catch(Exception ex)
            {
                return null;
            }
        }

        #region Dung cho Webservice
        //Lay danh sach Product: product_charid - name - by_product_catalouge
        public static List<BizProduct> getAll()
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var products = from p in db.Product
                               select new BizProduct(p.ProductCharID, p.Name, p.ByProductCatalogue);
                return products.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion
    }
}
